{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "1、句子的向量化\n",
    "\n",
    "举例："
   ],
   "id": "df558cf657495fb0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T14:46:31.498920Z",
     "start_time": "2025-09-10T14:46:28.867032Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "import os\n",
    "import dotenv\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv(\"OPENAI_API_KEY1\")\n",
    "os.environ['OPENAI_BASE_URL'] = os.getenv(\"OPENAI_BASE_URL\")\n",
    "\n",
    "embedding_model = OpenAIEmbeddings(\n",
    "    # model=\"text-embedding-ada-002\"\n",
    "    model = \"text-embedding-3-large\"\n",
    ")\n",
    "\n",
    "text = \"Nice to meet you!\"\n",
    "\n",
    "embed_query = embedding_model.embed_query(text = text,)\n",
    "\n",
    "print(len(embed_query))  # 1536 --> 3072\n",
    "\n",
    "print(embed_query[:10])"
   ],
   "id": "b0c501823fd5bd4d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3072\n",
      "[-0.027939368039369583, 0.03950421139597893, -0.020670808851718903, -0.0008068201714195311, 0.015577414073050022, -0.00793732050806284, -0.013834580779075623, 0.01602325402200222, 0.015712516382336617, 0.06263390183448792]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "2、文档的向量化\n",
    "\n",
    "文档的向量化，接收的参数是字符串数组。\n",
    "\n",
    "举例1：\n"
   ],
   "id": "869e4bb415af5bc4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T13:32:32.944790Z",
     "start_time": "2025-09-10T13:32:31.490884Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import dotenv\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = os.getenv(\"OPENAI_API_KEY1\")\n",
    "os.environ['OPENAI_BASE_URL'] = os.getenv(\"OPENAI_BASE_URL\")\n",
    "\n",
    "# 初始化嵌入模型\n",
    "embeddings_model = OpenAIEmbeddings(model=\"text-embedding-ada-002\")\n",
    "\n",
    "# 待嵌入的文本列表\n",
    "texts = [\n",
    "    \"Hi there!\",\n",
    "    \"Oh, hello!\",\n",
    "    \"What's your name?\",\n",
    "    \"My friends call me World\",\n",
    "    \"Hello World!\"\n",
    "]\n",
    "\n",
    "# 生成嵌入向量\n",
    "embeddings = embeddings_model.embed_documents(texts)\n",
    "\n",
    "\n",
    "for i in range(len(texts)):\n",
    "    print(f\"{texts[i]}:{embeddings[i][:3]}\",end=\"\\n\\n\")\n"
   ],
   "id": "71bbee1a76bcda1b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi there!:[-0.020325319841504097, -0.007096723187714815, -0.022839006036520004]\n",
      "\n",
      "Oh, hello!:[0.004446744918823242, -0.014353534206748009, 0.0019785689655691385]\n",
      "\n",
      "What's your name?:[-0.004887457471340895, -0.009618516080081463, 0.007236444391310215]\n",
      "\n",
      "My friends call me World:[-0.004588752053678036, -0.014497518539428711, 0.01022490207105875]\n",
      "\n",
      "Hello World!:[0.002393412170931697, 0.0002737858740147203, -0.0023398753255605698]\n",
      "\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "举例2：\n",
   "id": "2aa6abc0c7383184"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T13:35:02.567331Z",
     "start_time": "2025-09-10T13:35:01.595100Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from dotenv import load_dotenv\n",
    "from langchain_community.document_loaders import CSVLoader\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "\n",
    "\n",
    "embeddings_model = OpenAIEmbeddings(\n",
    "    model=\"text-embedding-3-large\",\n",
    ")\n",
    "\n",
    "# 情况1：\n",
    "loader = CSVLoader(\"./asset/load/03-load.csv\", encoding=\"utf-8\")\n",
    "docs = loader.load_and_split()\n",
    "\n",
    "#print(len(docs))\n",
    "\n",
    "# 存放的是每一个chrunk的embedding。\n",
    "embeded_docs = embeddings_model.embed_documents([doc.page_content for doc in docs])\n",
    "print(len(embeded_docs))\n",
    "# 表示的是每一个chrunk的embedding的维度\n",
    "print(len(embeded_docs[0]))\n",
    "print(embeded_docs[0][:10])"
   ],
   "id": "25454442b33af359",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "3072\n",
      "[0.0011534227523952723, 0.005336394999176264, -0.008949915878474712, 0.030998842790722847, -0.0017970810877159238, 0.011092216707766056, 0.021087471395730972, 0.048524416983127594, 0.0002677876618690789, -0.00012219828204251826]\n"
     ]
    }
   ],
   "execution_count": 5
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
